package com.lcf.feign.factory;

import com.lcf.feign.service.DictTableDynamicFeignClient;
import org.springframework.cloud.openfeign.FeignClientBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.lcf.base.constant.SysConstant.DICT_TABLE_DYNAMIC_FEIGN_PATH;

/**
 * 描述：表翻译动态Feign客户端工厂类
 *
 * @author lcf
 * @since 2025/3/31 22:01
 */
@Component
public class DictTableDynamicFeignClientFactory {
    private final ApplicationContext applicationContext;
    private final Map<String, DictTableDynamicFeignClient> clientCache = new ConcurrentHashMap<>();

    public DictTableDynamicFeignClientFactory(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public DictTableDynamicFeignClient getClient(String serviceName) {
        return clientCache.computeIfAbsent(serviceName, name -> {
            FeignClientBuilder builder = new FeignClientBuilder(applicationContext);
            return builder.forType(DictTableDynamicFeignClient.class, name)
                    .path(DICT_TABLE_DYNAMIC_FEIGN_PATH)
                    .build();
        });
    }
}
